<?php
class ModelWpPost extends Model {
	
	public function getMyPushPosts($customer_id,$pageNum,$pageSize){
		$sql = "select b.ID,b.post_title,b.post_modified,d.guid as thumb from customer_article a join wp_posts b on a.post_id = b.ID
				left join wp_postmeta c on b.ID = c.post_id and c.meta_key = '_thumbnail_id'
			    left join wp_posts d on c.meta_value = d.ID
				where a.customer_id =  {$customer_id} and a.avail_time < CURRENT_TIMESTAMP order by b.post_modified desc";
				
		if (isset($pageNum) || isset($pageSize)) {
			if ($pageNum< 0) {
				$pageNum = 0;
			}

			if ($pageSize < 1) {
				$pageSize = 20;
			}
			
			$sql .= " LIMIT " . (int)($pageNum*$pageSize) . "," . (int)$pageSize;
		}

		
		
		$query = $this->db->query($sql);

		return $query->rows;
	}
	
	public function getMySubscriptions($customer_id,$pageNum,$pageSize){
		$sql = "select b.ID,b.post_title,b.post_modified,
                b.publish_time,
                (select ifnull(max(avail_time),b.publish_time) from customer_article where customer_id = {$customer_id} and post_id = b.ID and avail_time < CURRENT_TIMESTAMP) as customer_publish_time,
                b.image as thumb from (
				(select DISTINCT a.ID 
				from wp_posts a 
				join wp_term_relationships b on a.ID=b.object_id 
				join wp_term_taxonomy c on b.term_taxonomy_id = c.term_taxonomy_id
				JOIN customer_wpcategory d ON c.term_id = d.category_id
				where d.customer_id = {$customer_id})
				union (select b.ID from customer_article a join wp_posts b on a.post_id = b.ID
				where a.customer_id =  {$customer_id} and a.avail_time < CURRENT_TIMESTAMP)
				) as a
				   join wp_posts b on a.ID = b.ID
				order by customer_publish_time desc ";

		if (isset($pageNum) || isset($pageSize)) {
			if ($pageNum< 0) {
				$pageNum = 0;
			}

			if ($pageSize < 1) {
				$pageSize = 20;
			}

			$sql .= " LIMIT " . (int)($pageNum*$pageSize) . "," . (int)$pageSize;
		}


		$query = $this->db->query($sql);

		return $query->rows;
	}

    public function get_article_list_by_term_id($term_id,$pageNum,$pageSize){
        $sql = "select b.ID,b.post_title,b.post_modified,b.image as thumb from 
				(select DISTINCT a.ID 
				from wp_posts a 
				join wp_term_relationships b on a.ID=b.object_id 
				join wp_term_taxonomy c on b.term_taxonomy_id = c.term_taxonomy_id and c.term_id={$term_id}
				
				) as a
				   join wp_posts b on a.ID = b.ID
				order by b.post_modified desc ";

        if (isset($pageNum) || isset($pageSize)) {
            if ($pageNum< 0) {
                $pageNum = 0;
            }

            if ($pageSize < 1) {
                $pageSize = 20;
            }

            $sql .= " LIMIT " . (int)($pageNum*$pageSize) . "," . (int)$pageSize;
        }



        $query = $this->db->query($sql);

        return $query->rows;
    }

	public function getSubscription($customer_id,$post_id){
		$this->db->query("update wp_posts set view_count = view_count+1 where ID={$post_id}");
		
		$this->db->query("update customer_article set `read` = 1,read_time=CURRENT_TIMESTAMP where post_id={$post_id} and customer_id={$customer_id} and read_time is null");



		$sql = "select * from wp_posts where ID =  {$post_id}  ";
		
		$query = $this->db->query($sql);

		$data = $query->rows;
		
		if(count($data)){
			
			
			$data[0]['comments'] = $this->getComments($post_id);
			
			return $data[0];
		}
		else
			return '';
	}
	
	
	public function getComments($post_id){
		$sql = "select a.*,b.image from wp_comments a left join customer b on a.user_id = b.customer_id where a.comment_post_ID =  {$post_id}  ";
				
		
		
		$query = $this->db->query($sql);

		return $query->rows;
		
	}
	
	public function comment($post_id,$message,$sender_id,$sender_name){
		$sql = "insert into wp_comments 
		set comment_post_ID={$post_id},
		comment_author='{$sender_name}',
		comment_date=CURRENT_TIMESTAMP,
		comment_date_gmt=CURRENT_TIMESTAMP, 
		comment_approved=1,
		comment_parent=0,
		user_id={$sender_id},
		comment_content='{$message}'";
				
		
		$query = $this->db->query($sql);

		
	}

	public  function getTop4Posts($customer_id){
        $sql = "select b.ID,b.post_title,b.post_modified,b.image as thumb from (
				(select DISTINCT a.ID 
				from wp_posts a 
				join wp_term_relationships b on a.ID=b.object_id 
				join wp_term_taxonomy c on b.term_taxonomy_id = c.term_taxonomy_id
				JOIN customer_wpcategory d ON c.term_id = d.category_id
				where d.customer_id = {$customer_id})
				union (select b.ID from customer_article a join wp_posts b on a.post_id = b.ID
				where a.customer_id =  {$customer_id} and a.avail_time < CURRENT_TIMESTAMP)
				) as a
				   join wp_posts b on a.ID = b.ID
				order by b.post_modified desc 
LIMIT 4";



        $query = $this->db->query($sql);

        return $query->rows;
    }

    public  function birthday_push(){

        $query = $this->db->query("select ID from wp_posts
          where post_type = 'special_message' and extra='用户生日'");

        foreach ($query->rows as $row){
            $post_id = $row['ID'];
            $sql = "
                insert into customer_message(customer_id,post_id,operator_id,avail_time)
                select customer_id,{$post_id},0,now() FROM
                (
                select customer_id,DATE_FORMAT(birthday,'%m-%d') from customer_child a
                where DATE_FORMAT(birthday,'%m-%d') = DATE_FORMAT(NOW(), '%m-%d')
                and not EXISTS 
                  (select 1 from customer_message b 
                  where a.customer_id = b.customer_id and b.post_id = {$post_id}
                  and DATE_FORMAT(b.send_time, '%Y-%m-%d') = DATE_FORMAT(NOW(), '%Y-%m-%d'))
                )a";
            $this->db->query($sql);
        }

        return 1;
    }
}